using Avalonia.Controls;
using Avalonia.Interactivity;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using MsBox.Avalonia;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Models;
using SchoolApp.Msgs;
using SchoolApp.ViewModels;

namespace SchoolApp.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = Program.ServiceProvider.GetRequiredService<MainViewModel>();
    }

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        WeakReferenceMessenger.Default.Register<ErrorMsg>(this, async (r, msg) =>
        {
            var messageBox = MessageBoxManager.GetMessageBoxCustom(
                new MessageBoxCustomParams
                {
                    ContentTitle = "错误提示",
                    ContentMessage = msg.Data,
                    ButtonDefinitions = [new ButtonDefinition() { Name = "Ok", IsDefault = true }],
                    Icon = MsBox.Avalonia.Enums.Icon.Error,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen, // 居中显示
                    Topmost = true,
                }).ShowWindowDialogAsync(this);
        });
    }

    private void MainWindow_OnUnloaded(object sender, RoutedEventArgs e)
    {
        WeakReferenceMessenger.Default.Unregister<ErrorMsg>(this);
    }
}